package com.zhangkeyou.map.mapweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.zhangkeyou.map.mapweb.domain.entity.Feature;
import com.zhangkeyou.map.mapweb.domain.entity.Features;
import com.zhangkeyou.map.mapweb.domain.entity.Status;
import com.zhangkeyou.map.mapweb.service.FeaturesService;
import com.zhangkeyou.map.mapweb.service.UserinfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/features")
public class FeaturesController {
    @Autowired
    private FeaturesService featuresService;
    @Autowired
    private UserinfoService userinfoService;

/*    *//**
     * 获取Features列表，前端递归调用
     * @param level
     * @param parentId
     * @return
     *//*
    @GetMapping
    public Status getFeaturesInfo(@RequestParam(name = "level")int level,@RequestParam(name="parentId",required = false) String parentId){
        Status featuresInfo = featuresService.getFeaturesInfo(level, parentId);
        return featuresInfo;
    }*/

    /**
     * 获取Features列表，后端的递归调用
     * @return
     */
    @GetMapping("/getfeaturelist")
    public Status getFeatureList() {
        List<Features> featureList = featuresService.getFeatureList();
        return new Status<>(200, featureList, "success");
    }

    /**
     * 获取featureId的Geometry
     * @param featureId
     * @return
     */
    @GetMapping("/getFeature")
    public Status getFeatureByFeatureId(@RequestParam(name="featureId") String featureId){
        Feature feature = featuresService.getFeatureByFeatureId(featureId);
        return new Status<>(200,feature,"success");
    }

    /**
     *
     * @param file 需要上传的文件
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('admin')")
    @PostMapping("/upload")
    public Status updateFeatures(@RequestParam("file") MultipartFile file) {
        Features features = featuresService.getFeaturesByFile(file);
        if(features!=null){
            featuresService.insertFeatures(features);
        }
        return new Status(200, null, "success");
    }

    /**
     * 适配el_tree动态加载数据
     * @param level
     * @param id 空，featuresId或者parentId
     * @return
     */
    @PreAuthorize("hasAnyAuthority('admin')")
    @GetMapping("/getFeatureListTree")
    public Status getFeatureListByLevelParentId(@RequestParam("level") int level,
                                                @RequestParam(value = "parentId",required = false) String id){
        List list = null;
        if(level==1){
            list = featuresService.getFirstLevelFeatures();
        } else if (level==2) {
            list = featuresService.getFeatureListByLevelAndFeaturesId(level-1,id);
        }else
            list = featuresService.getFeatureListByLevelParentId(level-1,id);
        return new Status<>(200,list,"success");
    }

    @PostMapping("/getFeaturesInfoByFile")
    public Status getFeaturesInfoByFile(@RequestParam("file") MultipartFile file){
        Features features = featuresService.getFeaturesInfoByFile(file);
        return new Status(200,features,"success");
    }

    @PreAuthorize("hasAnyAuthority('admin')")
    @DeleteMapping("/deleteByLevelAndId")
    public Status deleteByLevelAndId(@RequestParam("level") int level,@RequestParam(value = "id",required = false) String id){
        Object object = featuresService.deleteByLevelAndId(level, id);
        return new Status(200,object,"success");
    }

    @PreAuthorize("hasAnyAuthority('admin')")
    @PostMapping("/updateFeatureByJson")
    public Status updateFeatureByJson(@RequestBody JSONObject json){
        Object object = featuresService.updateFeatureByJson(json);
        return new Status<>(200,object,"success");
    }

    @PreAuthorize("hasAnyAuthority('admin')")
    @GetMapping("/updateFeaturesList")
    public Object updateFeaturesList(){
        Object object = featuresService.updateFeaturesList();

        return new Status<>(200,object,"success");
    }
}
